import numpy as np
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier

input_file = "car.data.txt"
X = []
count = 0
with open(input_file, "r") as f:
    for line in f.readlines():
        data = line[:-1].split(",")
        X.append(data)

X = np.array(X)

label_encoder = []
X_encoder = np.empty(X.shape)
for i, item in enumerate(X[0]):
    label_encoder.append(preprocessing.LabelEncoder())
    X_encoder[:, i] = label_encoder[-1].fit_transform(X[:, i])
X = X_encoder[:, :-1].astype(int)
y = X_encoder[:, -1].astype(int)

# params = {'n_estimators': 200,'max_depth':8,'random_state':7}
# classifier = RandomForestClassifier(**params)
# classifier.fit(X, y)
#
# from sklearn.model_selection import cross_val_score
# accuracy = cross_val_score(classifier, X, y, scoring='accuracy',cv=5)
# print(accuracy)

# input_data=['vhigh','vhigh','2','2','small','low']
# input_data_encoded = [-1]*len(input_data)
# for i, item in enumerate(input_data):
#     input_data_encoded[i] = label_encoder[i].transform([input_data[i]])[0]
# input_data_encoded = np.array(input_data_encoded)
# input_data_encoded = input_data_encoded.reshape(-1,6)
#
# output_class = classifier.predict(input_data_encoded)
# print(f'output class:{label_encoder[-1].inverse_transform(output_class)[0]}')

from sklearn.model_selection import validation_curve

classifier = RandomForestClassifier(n_estimators=20,random_state=7)
parameter_grid = np.linspace(2, 10, 5).astype(int)
train_scores, validation_scores = validation_curve(classifier, X, y, param_name="max_depth",
                                                   param_range=parameter_grid,cv=5)
print("### VALIDATION CURVES ###")
print(f"Param:n_estimators Training scores:{train_scores}")
print(f"Param:n_estimators Validation scores:{validation_scores}")

